package com.lapelife.backend.task;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lapelife.backend.common.RedisContains;
import com.lapelife.backend.mapper.PostUserMapper;
import com.lapelife.backend.model.domain.Post;
import com.lapelife.backend.model.domain.PostUser;
import com.lapelife.backend.service.PostService;
import com.lapelife.backend.service.PostUserService;
import com.lapelife.backend.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
@Slf4j
public class PostAutoMapperTask {
    private RedisUtil redisUtil;

    private PostService postService;

    private PostUserService postUserService;

    @Autowired
    public void setPostUserService(PostUserService postUserService) {
        this.postUserService = postUserService;
    }

    @Autowired
    public void setPostService(PostService postService) {
        this.postService = postService;
    }

    @Autowired
    public void setRedisUtil(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    // 每隔12小时执行一次
    @Scheduled(cron = "0 0 */12 * * ?")
    public void task() {
        Map<String, PostUser> postUserMap = new HashMap<>();
        Map<String, Post> postMap = new HashMap<>();

        // 收藏
        Set<String> collectKeys = redisUtil.keys(RedisContains.POST_COLLECT + "*");
        collectKeys.forEach(v -> {
            Long postId = Convert.convert(Long.class, v.split(RedisContains.POST_COLLECT)[1]);
            Set<Object> userIdList = redisUtil.sGet(v);

            // 添加文章收藏数量
            Post post = postMap.get(String.valueOf(postId));
            if (post == null) {
                post = new Post();
                post.setPostId(postId);
            }
            post.setCollects(userIdList.size());
            postMap.put(String.valueOf(postId), post);

            // 为所有用户添加文章收藏
            userIdList.forEach(p -> {
                PostUser postUser = postUserMap.get(p + "_" + postId);
                if (postUser == null) {
                    postUser = new PostUser();
                    postUser.setPostId(postId);
                    postUser.setUserId(Convert.convert(Long.class, p));
                }
                postUser.setCollectFlag("1");
                postUserMap.put(p + "_" + postId, postUser);
            });
        });

        // 点赞
        Set<String> thumbKeys = redisUtil.keys(RedisContains.POST_THUMBS + "*");
        thumbKeys.forEach(v -> {
            Long postId = Convert.convert(Long.class, v.split(RedisContains.POST_THUMBS)[1]);
            Set<Object> userIdList = redisUtil.sGet(v);

            // 添加文章收藏数量
            Post post = postMap.get(String.valueOf(postId));
            if (post == null) {
                post = new Post();
                post.setPostId(postId);
            }
            post.setThumbs(userIdList.size());
            postMap.put(String.valueOf(postId), post);

            // 为所有用户添加文章点赞
            userIdList.forEach(p -> {
                PostUser postUser = postUserMap.get(p + "_" + postId);
                if (postUser == null) {
                    postUser = new PostUser();
                    postUser.setPostId(postId);
                    postUser.setUserId(Convert.convert(Long.class, p));
                }
                postUser.setThumbFlag("1");
                postUserMap.put(p + "_" + postId, postUser);
            });
        });

        // 阅读
        Set<String> lookKeys = redisUtil.keys(RedisContains.POST_LOOK + "*");
        lookKeys.forEach(v -> {
            Long postId = Convert.convert(Long.class, v.split(RedisContains.POST_LOOK)[1]);
            Object num = redisUtil.get(v);

            // 添加文章浏览量
            Post post = postMap.get(String.valueOf(postId));
            if (post == null) {
                post = new Post();
                post.setPostId(postId);
            }
            post.setLooks(Convert.convert(Integer.class, num));
            postMap.put(String.valueOf(postId), post);
        });

        // 添加到数据库
        boolean b = postService.updateBatchById(postMap.values());
        log.info("同步文章信息到数据库：{}", b);
        int n = 0;
        for (PostUser value : postUserMap.values()) {
            n += postUserService.saveOrUpdate(value) ? 1 : 0;
        }
        log.info("同步文章用户关联信息到数据库：{}", n);
    }
}
